package yeepay

import (
	"github.com/gin-gonic/gin"
	"simplifly-tech/payment/consts"
	model2 "simplifly-tech/payment/model"
	"strconv"
	"strings"
	"time"
)

func (yeePayService *YeePayService) Notify(ctx *gin.Context) (*model2.PaymentNotification, error) {
	req := ctx.PostForm("response")
	content, err := yeePayService.decrypt(req)
	if nil != err {
		return nil, err
	}

	orderIdArr := strings.Split(content.OrderID, "-")
	status := consts.NotifyStatusFailed
	if content.Status == "SUCCESS" {
		status = consts.NotifyStatusSuccess
	}
	paidAt, _ := time.Parse("2006-01-02 15:04:05", content.PaySuccessDate)
	incomeId, _ := strconv.ParseUint(orderIdArr[1], 10, 64)

	return &model2.PaymentNotification{
		Status:       status,
		OrderID:      orderIdArr[0],
		IncomeID:     uint(incomeId),
		PaidAt:       paidAt,
		AsyncPayment: true,
		ReferenceID:  content.UniqueOrderNo,
	}, nil
}

//func (yeePayService *YeePayService) RefundNotify(req string) (*internal.PaymentNotification, error) {
//
//	content, err := yeePayService.decrypt(req)
//	if nil != err {
//		return nil, err
//	}
//
//	orderIdArr := strings.Split(content.OrderID, "-")
//	switch orderIdArr[0][0:1] {
//	case "F":
//		status := consts.NotifyStatusFailed
//		if content.Status == "SUCCESS" {
//			status = consts.NotifyStatusSuccess
//		}
//		paidAt, _ := time.Parse("2006-01-02 15:04:05", content.PaySuccessDate)
//		incomeId, _ := strconv.ParseUint(orderIdArr[1], 10, 64)
//
//		return &internal.PaymentNotification{
//			Status:       status,
//			OrderId:      orderIdArr[0],
//			IncomeId:     incomeId,
//			PaidAt:       paidAt,
//			AsyncPayment: true,
//		}, nil
//	}
//	return nil, nil
//}
